<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2019/2/19
 * Time: 16:46
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use zgldh\QiniuStorage\QiniuStorage;

use Validator;
//所有的验证权限 日志基类
class BaseApiController extends Controller
{
    public function __construct()
    {

    }

    public function log(){

    }
    //设置返回值格式
    public function result($code,$describe,$res)
    {
        $arr = [
            'code' => $code,
            'describe' => $describe,
            'res' => $res
        ];
        return json_encode($arr);
    }
    public function validator($data,$rules,$tips){
        return Validator::make($data,$rules,$tips);
    }
    /**
     * 判断id是否接收到
     */
    public function idFind($data){
        $rule=Validator::make($data,[
            'id'=>'required|int',
        ],[
            'id.required'=>'未接收到参数id！',
            'id.int'=>'id不是int型！',
        ]);
        return $rule;
    }

    //七牛云上传文件
    public function uploadFile(Request $request)
    {
        $file = $request->file('file');
        // 判断是否有文件上传
        if ($request->hasFile('file')) {
            // 初始化
            $disk = QiniuStorage::disk('qiniu');
            // 重命名文件
            if($_FILES['file']['type']=='video/mp4'){
                $fileName = 'video'.time().'.mp4';
            }elseif($_FILES['file']['type']=='audio/mp3'){
                $fileName = 'audio'.time().'.mp3';
            }else{
                $fileName = 'png'.time().'.png';
            }

            // 上传到七牛
            $bool = $disk->put($fileName,file_get_contents($file->getRealPath()));
            // 判断是否上传成功
            if ($bool) {
                $path = $disk->downloadUrl($fileName);
                $new_path = substr($path,0,strpos($path,'?'));
                return $new_path;
            }
            return '上传失败';
        }
        return '没有文件';

    }

}